package cn.iune.erp.purchase;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.iune.erp.setting.IBillLogService;

@Service
@Aspect//指定这是一个其切面
public class PurchaseOrderDaoAspect {
	
	@Autowired
	private IPurchaseOrderService purchaseOrderService;
	@Autowired
	private IBillLogService billLogService;
	
	//指定切入点（增加）
    @Pointcut("execution(* cn.iune.erp.purchase.IPurchaseOrderDao.insert(..)) && args(obj, ucode, uname, uid)")
    public void insertPointCut(PurchaseOrder obj, String ucode, String uname, String uid){}
    //指定通知以及通知的范围
    @Transactional
    @After("insertPointCut(obj, ucode, uname, uid)")
    public void afterInsert(PurchaseOrder obj, String ucode, String uname, String uid){

    }
    
	//指定切入点（删除）
    @Pointcut("execution(* cn.iune.erp.purchase.IPurchaseOrderDao.delete(..)) && args(fID, ucode, uname, uid)")
    public void deletePointCut(String fID, String ucode, String uname, String uid){}
    //指定通知以及通知的范围
    @Transactional
    @Before("deletePointCut(fID, ucode, uname, uid)")
    public void beforeDelete(String fID, String ucode, String uname, String uid){
    	PurchaseOrder obj = purchaseOrderService.getByID(fID);
		if("Y".equals(obj.getBillLog())){
			//登记日志
			billLogService.log(fID, "DELETE", ucode, uname, uid);
		}
    }
    
	//指定切入点（修改）
    @Pointcut("execution(* cn.iune.erp.purchase.IPurchaseOrderDao.update(..)) && args(obj, ucode, uname, uid)")
    public void updatePointCut(PurchaseOrder obj, String ucode, String uname, String uid){}
    //指定通知以及通知的范围
    @Transactional
    @Around("updatePointCut(obj, ucode, uname, uid)")
     public PurchaseOrder aroundUpdate(JoinPoint joinPoint, PurchaseOrder obj, String ucode, String uname, String uid) throws Throwable{
         System.out.println("环绕通知上。。。");
         
         PurchaseOrder objNew = (PurchaseOrder) ((ProceedingJoinPoint) joinPoint).proceed();
 		
         System.out.println("环绕通知下。。。");
         return objNew;
     }
    

}
